Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jcomte23/Python_vanilla/llms.txt
Use this file to discover all available pages before exploring further.
Introduction to Logical Operators
Logical operators allow you to combine multiple conditions in conditional statements. Python provides three logical operators: and, or, and not. These operators are fundamental for creating complex conditional logic.
The Three Logical Operators
# Relational operators
# >, <, >=, <=, ==, !=
# Mathematical operators
# +, -, *, /, //, **, %
# Logical operators
# and, or, not
Logical operators are primarily used in conditional structures to group multiple simple conditions together.
The AND Operator
The and operator returns True only if both conditions are True.
Truth Table for AND
| Condition 1 | Condition 2 | Result |
|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Basic AND Example
condicion1 = True
condicion2 = False
if condicion1 == True and condicion2 == True:
print("PRIMER BANDERA") # Won't execute
Practical AND Example: Login Validation
usuario = input("Usuario =>")
contraseña = input("Contraseña =>")
if usuario == "admin" and contraseña == "12345":
print("login exitoso")
else:
print("login erroneo")
With and, all conditions must be True for the entire expression to be True. If any condition is False, the whole expression is False.
Type Checking with AND
estatura = 4.0
# Both conditions must be True
print(estatura >= 1.71 and type(estatura) == float) # True
# One condition is False, so result is False
print(estatura >= 1.71 and isinstance(estatura, int)) # False
The OR Operator
The or operator returns True if at least one condition is True.
Truth Table for OR
| Condition 1 | Condition 2 | Result |
|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Basic OR Example
condicion1 = True
condicion2 = False
if condicion1 == True or condicion2 == True:
print("SEGUNDA BANDERA") # Will execute
Practical OR Example: Quarter Validation
print("validar si un mes esta en el primer trimestre del año")
mes = input("Mes =>")
if mes == "enero" or mes == "febrero" or mes == "marzo":
print("Corresponde al primer trimestre")
With or, only one condition needs to be True for the entire expression to be True. All conditions must be False for the expression to be False.
Type Checking with OR
estatura = 4.0
# At least one condition is True, so result is True
print(estatura >= 1.71 or isinstance(estatura, float)) # True
The NOT Operator
The not operator reverses the boolean value of a condition.
Truth Table for NOT
| Condition | Result |
|---|
| True | False |
| False | True |
Basic NOT Example
condicion1 = True
if not condicion1 == False:
print("TERCERA BANDERA") # Will execute
Practical NOT Example
variable = True
print(variable) # True
print(not variable) # False
is_logged_in = False
if not is_logged_in:
print("Please log in") # Will execute
not simply inverts the boolean value. not True becomes False, and not False becomes True.
Combining Logical Operators
You can combine multiple logical operators in a single expression:
Example 1: Access Control
age = 25
has_permission = True
is_banned = False
if (age >= 18 and has_permission) and not is_banned:
print("Access granted")
else:
print("Access denied")
Example 2: Complex Validation
score = 85
attempts = 2
max_attempts = 3
if score >= 80 or (score >= 60 and attempts < max_attempts):
print("You passed!")
else:
print("Try again")
Example 3: User Role Check
role = "admin"
is_active = True
if (role == "admin" or role == "moderator") and is_active:
print("You have elevated privileges")
Operator Precedence
When combining operators, Python follows this precedence order:
not (highest priority)
and
or (lowest priority)
# Without parentheses
result = True or False and False
print(result) # True (because 'and' is evaluated first)
# With parentheses for clarity
result = True or (False and False)
print(result) # True
result = (True or False) and False
print(result) # False
Use parentheses to make your intent clear, even when not strictly necessary. It improves code readability.
Practical Examples
Example 1: Authentication System
usuario = input("Usuario =>")
contraseña = input("Contraseña =>")
if usuario == "admin" and contraseña == "12345":
print("login exitoso")
else:
print("login erroneo")
Example 2: First Quarter Check
print("validar si un mes esta en el primer trimestre del año")
mes = input("Mes =>")
if mes == "enero" or mes == "febrero" or mes == "marzo":
print("Corresponde al primer trimestre")
else:
print("No corresponde al primer trimestre")
Example 3: Age and Permission Validation
age = 20
has_id = True
has_ticket = True
if age >= 18 and (has_id or has_ticket):
print("Entry allowed")
else:
print("Entry denied")
Example 4: System Status Check
is_online = True
is_maintenance = False
has_error = False
if is_online and not is_maintenance and not has_error:
print("System operational")
else:
print("System unavailable")
Short-Circuit Evaluation
Python uses short-circuit evaluation for logical operators:
AND Short-Circuit
# If first condition is False, second is never evaluated
if False and expensive_function():
print("This won't execute")
OR Short-Circuit
# If first condition is True, second is never evaluated
if True or expensive_function():
print("This will execute")
Short-circuit evaluation can improve performance by avoiding unnecessary evaluations.
Common Use Cases
email = input("Email: ")
password = input("Password: ")
if len(email) > 0 and len(password) >= 8:
print("Valid input")
else:
print("Invalid input")
Range Checking
value = 45
if value >= 0 and value <= 100:
print("Value is in valid range")
Multiple Choice Validation
response = input("Do you agree? (yes/y/si): ")
if response == "yes" or response == "y" or response == "si":
print("Agreement confirmed")
Best Practices
-
Use parentheses for clarity
# Good
if (age >= 18 and has_id) or is_vip:
grant_access()
# Less clear
if age >= 18 and has_id or is_vip:
grant_access()
-
Simplify complex conditions
# Good
is_valid_user = age >= 18 and has_permission
if is_valid_user and not is_banned:
grant_access()
# Less readable
if age >= 18 and has_permission and not is_banned:
grant_access()
-
Avoid redundant comparisons
# Good
if is_active:
do_something()
# Redundant
if is_active == True:
do_something()
Key Takeaways
and: Returns True only if all conditions are True
or: Returns True if at least one condition is True
not: Inverts the boolean value
- Operator precedence:
not > and > or
- Python uses short-circuit evaluation for efficiency
- Use parentheses to make complex conditions more readable
- Logical operators are essential for combining multiple conditions in
if statements